package com.cn.qgx428.lazydiary.ui.activity

import android.content.Context
import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.cn.qgx428.lazydiary.Presenter.PlanPresenter
import com.cn.qgx428.lazydiary.Presenter.UserPresenter
import com.cn.qgx428.lazydiary.R
import com.cn.qgx428.lazydiary.Utils.MConst
import com.cn.qgx428.lazydiary.Utils.SPUtil
import com.cn.qgx428.lazydiary.adapter.RAdapter
import com.cn.qgx428.lazydiary.adapter.RViewHolder
import com.cn.qgx428.lazydiary.dialog.CustomAlertDialog
import com.cn.qgx428.lazydiary.local.LAMessege
import com.cn.qgx428.lazydiary.local.LAction
import com.cn.qgx428.lazydiary.local.LActionWacher
import com.cn.qgx428.lazydiary.local.LManager
import com.cn.qgx428.lazydiary.network.Imageloader
import com.cn.qgx428.lazydiary.network.bean.Plan
import com.cn.qgx428.lazydiary.ui.activity.subActivity.PlanClockInActivity
import com.cn.qgx428.lazydiary.ui.activity.subActivity.PlanHistoryActivity
import com.cn.qgx428.lazydiary.ui.activity.subActivity.PlanModifyActivity
import kotlinx.android.synthetic.main.activity_plan.*
import org.jetbrains.anko.startActivity

/**
 * 该页面在登陆状态下才可使用
 */
class PlanActivity : BaseActivity(), LActionWacher {

    lateinit var dialog: CustomAlertDialog
    lateinit var planAdapter: PlanAdapter

    override fun initUI() {
        hindActionBar()
        setStatuBarColor(Color.BLACK)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_plan)
        LManager.newInstance().register(this, LAction.ACTION_LOGIN)
        // 检查登陆状态
        if (SPUtil.checkUserLogin()){
            initView()
            getPlanInfo()
        } else{
            prumpAlert()
        }
    }

    private fun getPlanInfo() {
        PlanPresenter.getPlans(SPUtil.currntUserId().toInt(), {
            if(it.plans != null)
                planAdapter.setDatas(it.plans.toMutableList())
        })
    }

    private fun initView() {
        plan_bar.setBackOnClickListener {
            finish()
        }
        plan_add.setOnClickListener({
            startActivity<PlanCreateActivity>()
            // 懒注册
            LManager.newInstance().register(this,LAction.ACTION_CREATE_PLAN)
        })
        plan_re.layoutManager = LinearLayoutManager(this)
        planAdapter = PlanAdapter(this, R.layout.adapter_plan_item)
        planAdapter.setDel {
            val dialog = getCustomAlertDialog()
            dialog.setMessage("确认删除计划吗")
            dialog.setPos {
                PlanPresenter.deletePlan(it,{
                    dialog.dismiss()
                    getPlanInfo()
                })
            }
            dialog.show()
        }
        plan_re.adapter = planAdapter
    }

    private fun prumpAlert() {
        dialog = getCustomAlertDialog()
        dialog.setMessage("请先登陆才能使用哦～")
        dialog.setNeg {
            dialog.dismiss()
            finish()
        }
        dialog.setPosText("登陆")
        dialog.setPos {
            startActivity<LoginActivity>()
            overridePendingTransition(R.anim._loginactivity_in, R.anim._activity_conteract)
        }
        dialog.show()
    }

    override fun onTrigger(action: String, msg: LAMessege) {
        when(action){
        /**
         * 登陆后更新页面状态
         */
            LAction.ACTION_LOGIN ->{
                dialog.dismiss()
                initView()
                getPlanInfo()
            }
            LAction.ACTION_CREATE_PLAN->{
                getPlanInfo()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        LManager.newInstance().unRegister(this)
    }

    class PlanAdapter: RAdapter<Plan>{

        var delListener: ((id: Int) -> Unit)? = null

        constructor(context: Context, layoutId: Int): super(context, layoutId)

        override fun conver(holder: RViewHolder, data: Plan) {
            val img = holder.getView<ImageView>(R.id.adapter_plan_img)
            val name = holder.getView<TextView>(R.id.adapter_plan_name)
            val time = holder.getView<TextView>(R.id.adapter_plan_startTime)
            val modify = holder.getView<ImageView>(R.id.adapter_plan_modify)
            val clockIn = holder.getView<ImageView>(R.id.adapter_plan_clockIn)
            val delete = holder.getView<ImageView>(R.id.adapter_plan_delete)
            val mainLalyout = holder.getView<RelativeLayout>(R.id.adapter_plan_itemLayout)

            Imageloader.download(mContext, img!! ,data.planimage)
            name?.text = data.planname
            time?.text = data.createtime
            modify?.setOnClickListener({
                mContext.startActivity<PlanModifyActivity>(
                        MConst.PLAN_ID to  data.id,
                        MConst.PlAN_IMAGE to data.planimage
                )
            })
            clockIn?.setOnClickListener({
                mContext.startActivity<PlanClockInActivity>(
                        MConst.PLAN_ID to data.id,
                        MConst.PLAN to data
                )
            })
            delete?.setOnClickListener({
                    delListener?.invoke(data.id)
            })
            mainLalyout?.setOnClickListener({
                mContext.startActivity<PlanHistoryActivity>(
                        MConst.PLAN_ID to data.id
                )
            })
        }

        fun setDel(listener: (id:Int)->Unit){
            delListener = listener
        }
    }
}
